package com.xiangxiangjd.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.xiangxiangjd.interceptor.AuthenticationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jinbin
 * @date 2018-07-08 22:33
 */
@Configuration
@Slf4j
public class InterceptorConfig implements WebMvcConfigurer {



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> exList=new ArrayList<>();

        //管理员权限放行
        exList.add("/**/admin/**");

        //后台登录页和首页
        exList.add("/login.html");
        exList.add("/index.html");


        exList.add("/star/user/login");

        exList.add("/star/questionnaire/userQuestionnaireAnswer");

        //静态资源
        exList.add("/error/**");
        exList.add("/static/**");
        exList.add("/fonts/**");
        exList.add("/index.html");
        exList.add("/favicon.ico");
        exList.add("/js/**");
        exList.add("/css/**");
        exList.add("/img/**");


        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(exList);
    }
    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }







    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("正在配置Long类型转换成String类型") ;
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        /**
         * 序列换成json时,将所有的long变成string
         * 因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);

    }

}
